package com.cloud.wyf.Util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cloud.wyf.Entity.User;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class GetTokenUtil {

    public static final String signnature = "wuyanfeng";
    public static final long EXPIRE = 1000*60*60*24;//有效期一天

    public static String getTokens(User login){
        JwtBuilder jwtBuilder= Jwts.builder();
        String jwtToken=jwtBuilder
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                .claim("id",login.getId())
                .claim("nickName",login.getNickname())
                .claim("realName",login.getRealname())
                .claim("role",login.getRole().toString())
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRE))
                .signWith(SignatureAlgorithm.HS256,signnature)
                .compact();
        return jwtToken;
    }

    public static String getInfoFromToken(String token,String str){
        DecodedJWT decode = JWT.decode(token);
        return decode.getClaim(str).asString();
    }
}
